# Contributor: Chocobo1 <https://github.com/Chocobo1>

_realname=qbittorrent
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=4.4.5
pkgrel=2
pkgdesc="An advanced BitTorrent client programmed in C++, based on Qt toolkit and libtorrent-rasterbar (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://qbittorrent.org/"
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-svg"
         "${MINGW_PACKAGE_PREFIX}-qt5-winextras"
         "${MINGW_PACKAGE_PREFIX}-libtorrent-rasterbar"
         "${MINGW_PACKAGE_PREFIX}-zlib")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: needed for torrent search tab")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools")
source=("https://downloads.sourceforge.net/sourceforge/qbittorrent/${_realname}-${pkgver}.tar.xz"{,.asc}
        001-PROCESS_CLASS_INFORMATION-require-win8.patch
        002-winconf-prepare-env-for-mingw.patch
        003-no-file-offset-bits-64.patch)
sha256sums=('0d6ed3948d4ad0d63f01510fe902639ae7b383ccfc1c01e88b75fba5b29ff3b3'
            'SKIP'
            'a89b2830259203734e30c02f40104bf2898ced576543a5b0edc559fe1884e0b4'
            '9a617170437982b7273264384e03e6dccf8a794356b7228e88ba6cc9cbaf9e80'
            'd1ff16cd4cd955532bc0db98c8f3714003e0dc1daaab8f2eee8c51c687ca413b')
validpgpkeys=('D8F3DA77AAC6741053599C136E4A2D025B7CC9A2') # sledgehammer_999 <hammered999@gmail.com>

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p0 -i ${srcdir}/001-PROCESS_CLASS_INFORMATION-require-win8.patch

  patch -Np1 -i "${srcdir}/002-winconf-prepare-env-for-mingw.patch"

  # This defines "stat" which clashes with libtorrent headers and breaks the build.
  # From what I see off_t isn't used in the code, so this shouldn't change anything
  patch -Np1 -i "${srcdir}/003-no-file-offset-bits-64.patch"
}

build() {
  cp -rf ${_realname}-${pkgver} build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-boost-system=boost_system-mt

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make INSTALL_ROOT=${pkgdir} install
  install -Dm644 "COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
